package com.hinnotek.amoeba.ui.activity.old

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.databinding.ActivityPtCreateUserBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaCustomerViewModel
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PTCreateUserActivity : BaseActivity<ActivityPtCreateUserBinding>() {

    private var customDialog: CustomDialog? = null

    val viewModel by viewModels<AmibaCustomerViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("创建客户")

        setContentView(R.layout.activity_pt_create_user)
    }

    override fun initVew() {
        bindingView.superButtonCreate.setOnClickListener(View.OnClickListener { v: View? ->
            if (ClickUtil.isFastClick()) {
                return@OnClickListener
            }
            val customerName = bindingView.etPtCreateUser.text.toString().trim()
            if (StringUtils.isEmpty(customerName)) {
                ToastUtils.show(mContext, "请填写客户名称")
                return@OnClickListener
            }
            val orgId = SharedPreUtils.getInteger("orgId", 0)
            val userName = SharedPreUtils.getString("userName")
            setAmoebaCustomer(customerName, orgId, userName)
        })
    }

    override fun setData() {}

    /**
     * app端添加客户接口
     * @param customerName
     * @param orgId
     * @param createUser
     */
    fun setAmoebaCustomer(customerName: String, orgId: Int, createUser: String) {
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
        viewModel.addCustomer(customerName, orgId, createUser)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    setResult(Activity.RESULT_OK)
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    ToastUtils.showShort(mContext, msg)
                    customDialog?.dismiss()
                    super.onError(msg, code)
                }
            })
    }
}